Repository navigation
fix: keep large Gmail searches from failing when one request runs out of rate-limit retries - #1357
Conversation
Requests already in flight when a pause starts come back rate-limited too. Escalating the shared streak for each of them pushed a single burst of concurrent rejections straight to the maximum backoff. Escalate only for requests sent after the last escalation; earlier responses extend the pause only when Google sends Retry-After.
Live runs against a real Gmail inbox showed shared pauses of 16-48s made 300-thread searches slower than short independent retries without adding reliability. Cap the exponential pause at 8s (Retry-After still up to 60s); a request that keeps getting rejected still waits more than a minute before its 12-response ceiling. Also replace the AGENTS.md instruction to use the removed committer helper.
|
🦞👀 Pull request received. I will update this pull request when review starts. ClawSweeper review completeClawSweeper finished reviewing this revision. The review result is being finalized. |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 7331fa0e83
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if resp.StatusCode == http.StatusTooManyRequests { | ||
| if _, _, err := peekResponseBody(resp); err != nil { | ||
| slog.Debug("could not buffer rate-limit response", "err", err) | ||
| } | ||
| } | ||
| lastRateLimit = resp |
There was a problem hiding this comment.
Close unbuffered rate-limit bodies before retrying
When a 429 body exceeds the 1 MiB inspection limit, peekResponseBody returns ok=false without closing the underlying body, but this code ignores that result and retains the live response while starting another attempt. Unlike the previous drainAndClose path, this prevents the connection from being released before retrying. I reproduced a blocked retry with a 2 MiB 429 response and an HTTP transport configured with MaxConnsPerHost: 1: the request fails at its context deadline instead of reaching the next successful response, and without a deadline it can hang indefinitely. Handle unsuccessful buffering explicitly so an unread network body is not retained across attempts.
Useful? React with 👍 / 👎.
|
Codex review: needs changes before merge. What this changesConcurrent Google API requests share rate-limit backoff, with a 12-response ceiling and a two-minute retry budget. Example: Run gog gmail search 'in:inbox' --max 300 against a rate-limited inbox.
Review scores
ProductKind: Bug fix · Worth it: Yes · Fix scope: Complete Merge readiness⛔ Needs changes before merge - 1 item remains Keep this PR open: it addresses a current Gmail reliability problem and has maintainer-approved direction, but an unclosed 429 response body can block retries. Reviewed head: Before merge
Findings
Agent review detailsHow this fits togetherThe shared HTTP retry transport receives authenticated Google API requests, coordinates retry waits, and returns responses or errors to commands such as Gmail search. flowchart TD
A[Gmail search command] --> B[Concurrent thread fetches]
B --> C[Shared retry transport]
C --> D[Rate limit pause]
D --> C
C --> E[Authenticated HTTP transport]
E --> F[Google API]
F --> C
Technical reviewBest possible solution: Retain the shared transport pause while ensuring every abandoned network response is closed before another attempt begins. Do we have a high-confidence way to reproduce the issue? Main’s independent six-retry budget and all-or-error Gmail fan-out explain the reported failure, and the body records an affected v0.48.1 run; this review ran no target code. Is this the best way to solve the issue? Coordinating backoff in the shared transport addresses the common cause without changing search output, provided unsuccessful response buffering releases its network resources. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning medium; reviewed against cfee06622536. Provenance checked
TestingProof path: unit tests only. SecurityNone. EvidenceAcceptance criteria:
What I checked:
Likely related people:
Preflight sourceSource unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified. Review metrics
LabelsLabel changes:
Label justifications:
Rating scale6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior. WorkflowClawSweeper edits this one comment on every review. Comment Reviewed November 27, 2026, 9:56 PM ET / November 28, 2026, 02:56 UTC. |
What Problem This Solves
Large Gmail searches such as
gog gmail search 'in:inbox' --max 300(or--allon a big inbox) can fail outright with403 rateLimitExceeded("Units per minute per user"). The search fetches thread details 10 at a time; each request backs off on its own, so a burst of rejections makes one unlucky request use up all six retries, and that single failure fails the whole search with no partial results.User Impact
Large Gmail searches and other concurrent Google API work now wait out per-minute quota pressure instead of failing. Rate-limited requests pause together (at most 8 seconds per pause unless Google sends
Retry-After, which is still honored up to 60 seconds) and a request only gives up after 12 rate-limit responses or a 2-minute budget. No flags or output change; the debug log message for a rate-limit wait is nowrate limited, waiting to retry.Why This Change Was Made
All requests on one transport now share a single pause: when any request is rate limited, new attempts and retries wait together and are released with a small random spread, instead of every worker retrying on its own schedule. The shared backoff escalates only for rejections from requests sent after the last escalation, so a single burst of concurrent rejections does not jump to the maximum pause. A first version with longer shared pauses (up to 48 seconds) was measurably slower against a real inbox, so the pause is capped at 8 seconds.
The Gmail guide also notes that
gmail messages search ... --ids-onlypiped togmail batch modifyuses far less quota than per-threadgmail thread modifyfor bulk label changes, and AGENTS.md no longer points to the removedcommitterhelper.Evidence
Live checks against a real Gmail account (one inbox, 300-thread
gmail search, 75-second cool-down before each run, verbose logs; no mail contents recorded):403 rateLimitExceeded, retries exhausted, no results)Across all heavy runs that day, 0.48.1 failed 1 of 5 times and used its final retry in 2 more; this branch completed every run with at least half of its rate-limit allowance left. When 0.48.1 succeeds it is somewhat faster in this small sample (median about 36 s vs 52 s), with wide run-to-run variance (21–76 s).
Local:
make lint(0 issues),go test -race ./internal/googleapi/..., the focused rate-limit tests 30 times under-race,go test ./..., andmake testpass. New unit tests cover the shared pause, release spread, streak escalation and decay, in-flight rejection waves, the time budget and response ceiling,Retry-After,WithoutRetries, and cancellation while waiting.make ciwas not run locally; PR CI covers it.